from data_resource.data_process import run_daily, run_weekend
from utilities.utilities_func import init_ts
from apscheduler.schedulers.blocking import BlockingScheduler
import datetime


def daily_process():
    pro = init_ts()
    today = datetime.datetime.today().date().strftime("%Y%m%d")
    today1 = datetime.date.today()
    start = today1 - datetime.timedelta(days=14)
    _x = pro.trade_cal(exchange='SSE', start_date="20250926", end_date="20250926")

    if _x.empty:
        text_output = "交易日判断未返回"
        print(text_output)
    elif _x.iloc[0, 2] == 1:
        run_daily(start, today)

    print(f"{today} 任务完成")


def weekend_process():
    run_weekend()


if __name__ == '__main__':
    # 创建调度器
    scheduler = BlockingScheduler()

    # 添加每日18点执行的任务
    scheduler.add_job(daily_process, 'cron', hour=16, minute=10, misfire_grace_time=5)

    # 添加每周六9点执行
    scheduler.add_job(weekend_process, 'cron', day_of_week='sat', hour=9, minute=10, misfire_grace_time=5)

    # 启动调度器
    scheduler.start()
